//
//  PollingProgressView.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/5.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "PollingProgressView.h"
#import "PollingProgressCell.h"
#import "PollingProgressModel.h"
#import "MakePhoneView.h"
#define MINIMUM_ZOOM_ARC 0.014 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.5
#define MAX_DEGREES_ARC 360


@interface PollingProgressView(){
    
    NSMutableDictionary *_dataDic;
    dispatch_source_t _timer;
}
@property (nonatomic, strong) NSString*orderUuid;

@end
@implementation PollingProgressView
-(instancetype)initWithFrame:(CGRect)frame withWarningUuid:(NSString *)warningUuid
{
    self = [super initWithFrame:frame];
    if (self) {
        _orderUuid=warningUuid;
        _dataDic =[[NSMutableDictionary alloc]init];
        [self setShowNoDataNotice:@""];
        
        self.backgroundColor =[UIColor clearColor];
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator =NO;
        [self registerCell:[PollingProgressCell class] withModel:[PollingProgressModel class]];
        //死的
        [self setDataWithUrl:@"inspection_pagedOperationst" andParam:@{@"uuid":_orderUuid}];
        [self setTableActionType:CTableActionType_Both];
//        [self loadData];
        [self refresh];

        
        
    }
    return self;
    
}

- (void)refresh{
    [CClass HttpRequestTask:NO withUrl:@"inspection_pagedOperationst" withData:@{@"uuid":_orderUuid} notifyError:NO onSuccess:^(BaseModel *model) {
        [self reloadData];
        
    } onError:^(BaseModel *model) {
        
    }];
    [super refresh];
}

- (void)reloadData
{
    //    NSInteger click = 0;
    
    for (int i=0; i<self.mainData.count; i++) {
        PollingProgressModel *model = [self.mainData objectAtIndex:i];
        NSInteger num =[model.P_state integerValue];
        //评价不显示
        if (num==99) {
            [self.mainData removeObjectAtIndex:i];
        }
        
        
        
    }
    
    [super reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PollingProgressModel *model = [self.mainData objectAtIndex:indexPath.row];
    NSInteger num=[model.P_state integerValue];
    CGFloat width=10+35+14;
    
    //分配  确认
    if(num==1||num==90)
        
    {
        CGSize labelsize = CGSizeZero;
        
        if ([model.P_content isKindOfClass:[NSNull class]]||[model.P_content isEqualToString:@""]) {
            
        }else
        {
            
            labelsize = [model.P_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-width, 900)];
            
        }
        return MAX((labelsize.height+10) + 65-14, 65);
    
        
    }else
    {
        return  65;
        
    }
    
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PollingProgressCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; //出列可重用的
    if (cell == nil) {
        cell = [[PollingProgressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    cell.backgroundColor =[UIColor clearColor];
    PollingProgressModel *model1 = [self.mainData objectAtIndex:0];
    [cell setModel:model1];
    
    cell.backgroundColor =[UIColor whiteColor];
    if (self.mainData.count==1) {
        cell.isRow=YES;
    }else
    {
        cell.isRow=NO;
        
    }
    PollingProgressModel *model = [self.mainData objectAtIndex:indexPath.row];
    [cell setModel:model];
    if (indexPath.row==self.mainData.count-1) {
        cell.lineImg.hidden=YES;
    }else
    {
        cell.lineImg.hidden=NO;
        
    }
    if(indexPath.row==0)
    {
        cell.topLine.image=[UIImage imageNamed:@""];
    }
    NSInteger num=[model.P_state integerValue];
   
    //显示备注的分配、确认
    if(num==1||num==90)
    {
        cell.contentLab.text= model.P_content;
        
        
    }
    //90确认 91退回
    if(num==91)
    {
        cell.nameLab.text= model.P_customer_name;
        cell.nameLab.textColor=MainColor;

    }else if(num==90)
    {
        cell.nameLab.text= model.P_customer_name;
        cell.nameLab.textColor=LightFontColor;
    }else
    {
        cell.nameLab.textColor=MainColor;

    }
    
      //10接单
    if (num==10) {
        if ([model.P_customer_mobile isEqualToString:[CClass getCacheData:NSUD_Mobile]]) {
            
        }else
        {//代替别人之后可以看到代替的人接单
            cell.nameLab.text=[NSString stringWithFormat:@"%@",model.P_customer_mobile];
            UIImage*phoneImage=[UIImage imageNamed:@"Ico_home_detailPhone"];
            cell.phoneImg.image=phoneImage;
            
            [cell.phoneView bk_whenTapped:^{
                
                [Common makePhoneWith:model.P_customer_mobile];
                
                
                
            }];
        }
       
    }else if(num==12)
    {
        if ([model.P_customer_mobile isEqualToString:[CClass getCacheData:NSUD_Mobile]]) {
            //开始巡检
           
        }else
        {//被代替之后可以看到别人巡检
            cell.nameLab.text=[NSString stringWithFormat:@"%@",model.P_customer_mobile];
            UIImage*phoneImage=[UIImage imageNamed:@"Ico_home_detailPhone"];
            cell.phoneImg.image=phoneImage;
            
            [cell.phoneView bk_whenTapped:^{
                
                [Common makePhoneWith:model.P_customer_mobile];
                
                
            }];

        }
 
    }else
    {
        
    }
    
    return cell;
    
    
}


@end
